package com.founder.android.parser;

import com.founder.android.data.GeoData;

import android.sax.Element;
import android.sax.EndTextElementListener;
import android.sax.RootElement;

/**
 * 
 * @author chen_weihua
 * <GeocodeResponse>
 * 	<status>OK</status>
 * 	<result>
 * 	  <type>route</type>
 * 	  <formatted_address>中国江苏省苏州市吴中区艺坊路</formatted_address>
 * 	  <address_component>
 * 	    <long_name>艺坊路</long_name>
 * 	    <short_name>艺坊路</short_name>
 * 	    <type>route</type>
 * 	  </address_component>
 * 	  <address_component>
 * 	    <long_name>吴中区</long_name>
 * 	    <short_name>吴中区</short_name>
 * 	    <type>sublocality</type>
 * 	    <type>political</type>
 * 	  </address_component>
 * 	  <address_component>
 * 	    <long_name>苏州</long_name>
 *      <short_name>苏州</short_name>
 *      <type>locality</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>江苏省</long_name>
 *      <short_name>江苏省</short_name>
 *      <type>administrative_area_level_1</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>中国</long_name>
 *      <short_name>CN</short_name>
 *      <type>country</type>
 *      <type>political</type>
 *    </address_component>
 *    <geometry>
 *      <location>
 *        <lat>31.2658629</lat>
 *        <lng>120.7333819</lng>
 *      </location>
 *      <location_type>APPROXIMATE</location_type>
 *      <viewport>
 *        <southwest>
 *          <lat>31.2645118</lat>
 *          <lng>120.7300108</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>31.2672097</lat>
 *          <lng>120.7367550</lng>
 *        </northeast>
 *      </viewport>
 *      <bounds>
 *        <southwest>
 *          <lat>31.2647518</lat>
 *          <lng>120.7300108</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>31.2669697</lat>
 *          <lng>120.7367550</lng>
 *        </northeast>
 *      </bounds>
 *    </geometry>
 *  </result>
 *  <result>
 *    <type>sublocality</type>
 *    <type>political</type>
 *    <formatted_address>中国江苏省苏州市吴中区</formatted_address>
 *    <address_component>
 *      <long_name>吴中区</long_name>
 *      <short_name>吴中区</short_name>
 *      <type>sublocality</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>苏州</long_name>
 *      <short_name>苏州</short_name>
 *      <type>locality</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>江苏省</long_name>
 *      <short_name>江苏省</short_name>
 *      <type>administrative_area_level_1</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>中国</long_name>
 *      <short_name>CN</short_name>
 *      <type>country</type>
 *      <type>political</type>
 *    </address_component>
 *    <geometry>
 *      <location>
 *        <lat>31.2627910</lat>
 *        <lng>120.6319780</lng>
 *      </location>
 *      <location_type>APPROXIMATE</location_type>
 *      <viewport>
 *        <southwest>
 *          <lat>30.9257058</lat>
 *          <lng>119.9191120</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>31.4309054</lat>
 *          <lng>120.9015110</lng>
 *        </northeast>
 *      </viewport>
 *      <bounds>
 *        <southwest>
 *          <lat>30.9257058</lat>
 *          <lng>119.9191120</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>31.4309054</lat>
 *          <lng>120.9015110</lng>
 *        </northeast>
 *      </bounds>
 *    </geometry>
 *  </result>
 *  <result>
 *    <type>locality</type>
 *    <type>political</type>
 *    <formatted_address>中国江苏省苏州市</formatted_address>
 *    <address_component>
 *      <long_name>苏州</long_name>
 *      <short_name>苏州</short_name>
 *      <type>locality</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>江苏省</long_name>
 *      <short_name>江苏省</short_name>
 *      <type>administrative_area_level_1</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>中国</long_name>
 *      <short_name>CN</short_name>
 *      <type>country</type>
 *      <type>political</type>
 *    </address_component>
 *    <geometry>
 *      <location>
 *        <lat>31.2988860</lat>
 *        <lng>120.5853160</lng>
 *      </location>
 *      <location_type>APPROXIMATE</location_type>
 *      <viewport>
 *        <southwest>
 *          <lat>31.1336138</lat>
 *          <lng>120.3744507</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>31.5230825</lat>
 *          <lng>120.9141541</lng>
 *        </northeast>
 *      </viewport>
 *      <bounds>
 *        <southwest>
 *          <lat>30.7578406</lat>
 *          <lng>119.9191120</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>32.0458273</lat>
 *          <lng>121.3364320</lng>
 *        </northeast>
 *      </bounds>
 *    </geometry>
 *  </result>
 *  <result>
 *    <type>administrative_area_level_1</type>
 *    <type>political</type>
 *    <formatted_address>中国江苏省</formatted_address>
 *    <address_component>
 *      <long_name>江苏省</long_name>
 *      <short_name>江苏省</short_name>
 *      <type>administrative_area_level_1</type>
 *      <type>political</type>
 *    </address_component>
 *    <address_component>
 *      <long_name>中国</long_name>
 *      <short_name>CN</short_name>
 *      <type>country</type>
 *      <type>political</type>
 *    </address_component>
 *    <geometry>
 *      <location>
 *        <lat>32.0617070</lat>
 *        <lng>118.7632320</lng>
 *      </location>
 *      <location_type>APPROXIMATE</location_type>
 *      <viewport>
 *        <southwest>
 *          <lat>30.7578406</lat>
 *          <lng>116.3619604</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>35.1245136</lat>
 *          <lng>121.9321143</lng>
 *        </northeast>
 *      </viewport>
 *      <bounds>
 *        <southwest>
 *          <lat>30.7578406</lat>
 *          <lng>116.3619604</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>35.1245136</lat>
 *          <lng>121.9321143</lng>
 *        </northeast>
 *      </bounds>
 *    </geometry>
 *  </result>
 *  <result>
 *    <type>country</type>
 *    <type>political</type>
 *    <formatted_address>中国</formatted_address>
 *    <address_component>
 *      <long_name>中国</long_name>
 *      <short_name>CN</short_name>
 *      <type>country</type>
 *      <type>political</type>
 *    </address_component>
 *    <geometry>
 *      <location>
 *        <lat>35.8616600</lat>
 *        <lng>104.1953970</lng>
 *      </location>
 *      <location_type>APPROXIMATE</location_type>
 *      <viewport>
 *        <southwest>
 *          <lat>18.1535613</lat>
 *          <lng>73.4994137</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>53.5609740</lat>
 *          <lng>134.7728100</lng>
 *        </northeast>
 *      </viewport>
 *      <bounds>
 *        <southwest>
 *          <lat>18.1535613</lat>
 *          <lng>73.4994137</lng>
 *        </southwest>
 *        <northeast>
 *          <lat>53.5609740</lat>
 *          <lng>134.7728100</lng>
 *        </northeast>
 *      </bounds>
 *    </geometry>
 *  </result>
 * </GeocodeResponse>
 */
public class GeocodeXMLParserHandler implements XmlParserHandler {
	
	public static String RESPONSE_CHARSET = "utf-8";
	
	private static final String ITEM = "result";
	
	private static final String TYPE = "type";
	
	private static final String ADDRESS = "formatted_address";
	
	public static String getUrl(double lat, double lng) {
		return String.format(
				"http://maps.google.com/maps/api/geocode/xml?latlng=%.6f,%.6f&sensor=true&language=zh-CN", 
				lat, lng);
	}

	@Override
	public RootElement getRootElement() {
		return new RootElement("GeocodeResponse");
	}
	
	@Override
	public Copyable getPraseData() {
		return new GeoData();
	}
	
	@Override
	public Element getItem(RootElement root) {
		Element item = root.getChild(ITEM);
		return item;
	}
	
	@Override
	public void config(Element item, Copyable data) {
		final GeoData geoData = (GeoData)data;
		item.getChild(TYPE).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body) {
				geoData.addType(body);
			}
		});
		item.getChild(ADDRESS).setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body) {
				geoData.setAddress(body);
			}
		});
		Element location = item.getChild("geometry").getChild("location");
		location.getChild("lat").setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body) {
				geoData.setLat(body);
			}
		});
		location.getChild("lng").setEndTextElementListener(new EndTextElementListener() {
			@Override
			public void end(String body) {
				geoData.setLng(body);
			}
		});
	}

}
